banner2.admin = liwe.module ( "banner2.admin", null, "Gestione Banner" );

banner2.admin._dest_div = 'system-content';

banner2.admin._opts = [];

banner2.admin._effect_opts = [ 	{ 'label' : 'Swap', 'value' : 0 },
				{ 'label' : 'Crossfade', 'value' : 1 },
				{ 'label' : 'Slide Right', 'value' : 2 },
				{ 'label' : 'Slide Top', 'value' : 3 },
				{ 'label' : 'Slide Left', 'value' : 4 },
				{ 'label' : 'Slide Bottom', 'value' : 5 }
			    ];

//{{{banner2.admin._init_admin  = function ()
banner2.admin._init_admin  = function ()
{
	var _acts = [
		{ label: 'Banner', func: function () { banner2.admin._render_banners (); } },
		{ label: 'Gruppi', func: function () { banner2.admin._render_gruppi (); } },
		{ label: 'Tipi', func: function () { banner2.admin._render_tipi (); } },
		{ label: 'Statistiche', func: function () { banner2.admin._render_stats (); } }
	];

        var _plugin = [
                {
                        label:   "Inserisci banner",
                        handler: banner2.admin._jconed_handler,
                        context: banner2.admin._jconed_context
                }
        ];


	banner2.admin.register ( _acts, _plugin );
	banner2.admin._cache_opts ();
	banner2.admin._am_banner_init ( 'bn-admin-list' );
	banner2.admin._am_gruppo_init ( 'bn-admin-list' );
	banner2.admin._am_tipo_init ( 'bn-admin-list' );
	stats.init ( 'banner', { 'View': 'view', 'Click': 'click' } );
};
//}}}
//{{{banner2.admin.init = function ( dest_div )
banner2.admin.init = function ( dest_div )
{
        //if ( ! dest_div ) banner2.admin._set_dest_div ( banner2.admin._dest_div );
        if ( dest_div ) banner2.admin._set_dest_div ( dest_div );

        // no longer supported banner2.admin._get_tags_list ();

        if ( ! banner2.templates )
        {
                banner2.load_templates ( banner2.admin._init_admin );
                return;
        }
        banner2.admin._init_admin ();
};
//}}}
//{{{banner2.admin._cache_opts =  function ()
banner2.admin._cache_opts =  function ()
{
	banner2.admin._opts = [];
	var actions =[ { action : 'banner2.ajax.list_gruppo' , '_X_LINES' : -1 },
	  { action : 'banner2.ajax.list_tipo' , '_X_LINES' : -1 } ];
	
	actions.iterate ( function ( act )
	{	
		var tipo = act.action.split( '_' ) [ 1 ];
		liwe.AJAX.easy ( act , function ( data ) 
		{
			banner2.admin._opts [ tipo ] = banner2.admin._create_opts ( data );
 		} );
	} );
};
//}}}
//{{{banner2.admin._create_opts = function ( v )
banner2.admin._create_opts = function ( v )
{
	var res = [];
	v.rows.iterate ( function ( row )
	{
		res.push ( { 'label': row.name, 'value': row.id } );
		
	} );
	return res;
};
//}}}
//{{{ RENDER LAYOUTS
//{{{banner2.admin._render_gruppi = function ()
banner2.admin._render_gruppi = function ()
{
	banner2.admin._render_panel ( 'bn-admin-content', 'Gruppi', function ()
	{
		banner2.admin._gruppo_refresh();
	});
};
//}}}
//{{{banner2.admin._render_tipi = function ()
banner2.admin._render_tipi = function ()
{
	banner2.admin._render_panel ( 'bn-admin-content', 'Tipologie', function ()
	{
		banner2.admin._tipo_refresh();
	});
};
//}}}
//{{{banner2.admin._render_banners = function ()
banner2.admin._render_banners = function ()
{
	banner2.admin._render_panel ( 'bn-admin-content', 'Banners', function ()
	{
		banner2.admin._banner_refresh ();
		window.location.hash = "#main-container";
	});
};
//}}}
//{{{banner2.admin._render_panel = function ( panel, title, cback )
banner2.admin._render_panel = function ( panel, title, cback )
{
        var dct = { '_panel' : banner2.templates [ panel ], '_title' : title };
        $( banner2.admin._dest_div, String.formatDict ( banner2.templates [ 'bn-admin-main' ], dct ) );
        if ( cback ) cback();
};
//}}}
//}}}
//{{{ GESTIONE BANNER
//{{{banner2.admin._am_banner_init = function ( dest_div )
banner2.admin._am_banner_init = function ( dest_div )
{
        if ( banner2.admin.am_banner ) return;
        banner2.admin.am_banner = new AdminManager
                                ( {
                                        'dest_res': dest_div,
                                        'name' : 'bn_am_banner',
                                        'list_action': "banner2.ajax.list_banner",
                                        'del_action':  "banner2.admin.del_banner",
                                        'add_action':  "banner2.admin.add_banner",
                                        'edit_action':  "banner2.admin.edit_banner",
                                        'mod_menu_actions': [
                                                                 { func: "banner2.admin.add_banner()", 'class' : 'add' , label: 'Aggiungi Banner' }
					],
                                        'filter_form' : banner2.admin._am_banner_filter_form,
                                        'filter_form_name' : "bn-banner-filter"
                                } );

        banner2.admin.am_banner.set_row_fields (
                                [
                                        [ "id", "id_banner" ],
                                        [ "Stato", "_state" ],
                                        [ "Descrizione", "descr" ],
                                        [ "Tipo", "tipo" ],
                                        [ "Gruppo", "gruppo" ],
                                        [ "Inizio", "data_on" ],
                                        [ "Fine", "data_off" ],
                                        [ "Peso", "_peso" ],
					[ "Tipo", "_ext" ],
                                        [ "Creato", "_created" ]
                                ]
                                );

        banner2.admin.am_banner.cbacks [ 'row_manip' ] = banner2.admin._rwman_banner;
        banner2.admin.am_banner.cbacks [ 'del_item' ]  = function ( id ) { banner2.admin.del_banner ( id ); };
        banner2.admin.am_banner.cbacks [ 'edit_item' ]  = function ( id ) { banner2.admin.edit_banner ( id ); };
        banner2.admin.am_banner.cbacks [ 'noresult' ] = function () { banner2.admin.add_banner (); };

};
//}}}
//{{{banner2.admin._rwman_banner = function ( ds, row )
banner2.admin._rwman_banner = function ( ds, row )
{
	row [ "_id" ] = row [ 'id_banner' ];
	//row [ "_thumb" ] = String.formatDict ( '<img src="/site/banner/thumbs/%(id_banner)s.jpg" />', row );
	row [ "_state" ] = banner2.admin._check_state ( row [ 'data_on' ], row [ 'data_off' ] );
	row [ "_peso" ] = String.formatDict ( '<div style="text-align:right">%(peso)s </div>', row );
	row [ "_ext" ] = String.formatDict ( banner2.templates [ 'bn-admin-image-ico' ],  row  );
	row [ "_created" ] = row [ 'created' ].split( " ") [ 0 ] 
};
//}}}
//{{{banner2.admin._am_banner_filter_form = function ( dest_div )
banner2.admin._am_banner_filter_form = function ( dest_div )
{
	var opts = [	{ 'label' : 'Tutti', 'value' : 0 },
			{ 'label' : 'Scaduti', 'value' : -1 },
			{ 'label' : 'Scadenza 5 gg', 'value' : 5 },
			{ 'label' : 'Scadenza 15 gg', 'value' : 15 },
			{ 'label' : 'Scadenza 30 gg', 'value' : 30 },
			{ 'label' : 'Scadenza 60 gg', 'value' : 60 }
		   ];		 

	var f = new liwe.form.instance ( 'bn-banner-filter' );
	f.select ( { label : "Gruppo", name: 'id_gruppo',  options: banner2.admin._opts [ 'gruppo' ], nonl: true, force_select: true } );
	f.select ( { label : "Tipo", name: 'id_tipo',  options: banner2.admin._opts [ 'tipo' ], nonl: true, force_select: true } );
	f.text ( { label : "Descrizione", name : "descr", nonl : true } );
	f.calendar ( { label : "Data", name: 'data', title: 'Data', nonl : true } );
	f.select ( { label : "Scadenza", name: 'giorni',  options: opts, value : 0  } );
	f.button ( { value: "Applica Filtro", "class" : "green", onclick: "banner2.admin._am_banner_filter()", nonl: true } );
	f.button ( { value: "Svuota", "class": "yellow", onclick: "banner2.admin._banner_refresh()" } );
	f.set ( dest_div );
};
//}}}
//{{{banner2.admin._am_banner_filter = function ()
banner2.admin._am_banner_filter = function ()
{
	banner2.admin.am_banner.search_start ();
};
//}}}
//{{{banner2.admin._banner_form = function ( vals )
banner2.admin._banner_form = function ( vals )
{
	if ( ! vals ) vals = {};

	var f = new liwe.form.instance ( 'bn-banner-form' );

	f.events [ 'complete' ] = banner2.admin._banner_save;
	f.events [ 'start' ] = banner2.admin._add_date;
	f.hidden ( "action", 'banner2.admin.add_banner' );
	f.hidden ( 'id_banner', vals.get ( 'id' , 0  ) );
	f.hidden ( 'date', vals.get ( 'date' , "" ) );
	if ( vals.get ( 'id' , 0 ) == 0 ) 
	{
		f.uploader ( { label : "Immagine" , name: 'upload', mandatory : true } );
	};
	f.text ( { label : "Descrizione", name: 'descr', value : vals.get ( 'descr', '' ) } );
	f.select ( { label : "Gruppo", name: 'id_gruppo', value: vals.get ( 'id_gruppo', "" ), options: banner2.admin._opts [ 'gruppo' ], mandatory: true, force_select: true } );
	f.select ( { label : "Tipo", name: 'id_tipo', value: vals.get ( 'id_tipo', "" ), options: banner2.admin._opts [ 'tipo' ], mandatory: true, force_select: true } );
	f.number ( { label : "Peso", name: 'peso', value : vals.get ( 'peso', 1 ) } );
	f.calendar ( { label : "Data inizio", name: 'data_on', title: 'Data inizio' ,time : true, nonl : true, value : vals.get ( 'data_on', Date.formatted ( '%Y-%m-%d' ) )} );
	f.calendar ( { label : "Data Fine", name: 'data_off', title: 'Data fine', time : true, value : vals.get ( 'data_off', Date.formatted ( '%Y-%m-%d' ) ) } );
    f.label ( { value: banner2.templates [ 'admin-calendar-layout' ], colspan : 3 } );
	f.text ( {label : "Link", colspan : 3, size : 50,  name: 'lnk', value : vals.get ( 'lnk', '' )  } );
	f.tags ( { label: 'Tags', name: 'tags', module_name: "banner", 'value' : tags.convert ( vals.get ( 'tags' , '' )  ) } );	
    f.submit ( { value: 'Salva', "class" : "button green",  nonl : true } );
    f.button ( { value: 'Annulla', "class" :"grey",  onclick : "banner2.admin._banner_refresh()" } );
    f.sep ();
    f.workspace ( { name: "bn-image-thumb" } );
	f.set ( 'bn-admin-form' );
	banner2.admin._set_date( f );
	$ ( "bn-admin-rtitle", "Inserimento" );
};
//}}}
banner2.admin._add_date = function ( frm )
{
	
	frm.set_value ( "date" , "" );
	var _date =  banner2.admin._get_date ();
	if ( _date.count() == 0 ) return true;
	frm.set_value ( "date" , _date.join ( "|" ) );
	return true;
};
banner2.admin._get_date = function ()
{
    var div = $( "bn_calendar" );
    var elems = div.getElementsByTagName ( "input" );
    var t, l = elems.length, el;
    var res = [];

    for ( t = 0; t < l; t ++ )
    {
        el = elems [ t ];
        if ( el.type != "checkbox" ) continue;
        if ( el.checked == false ) continue;
        if ( el.value == 'on' ) continue;
		
		var x =  el.value.split ( "/" );
		var dt = new Date ( x [ 2 ], x [ 1 ]-1, x [ 0 ] );
		
        res.push ( dt.format_date ( "%Y-%m-%d") );
    }
    return res;
};

banner2.admin._check_state = function ( date_start, date_end )
{
	//0: passed, 1: to come, 2: in range
	var today = Date.parse ( new Date () );
	date_start = Date.parse ( date_start.split ( ' ' )[ 0 ] );
	date_end = Date.parse ( date_end.split ( ' ' )[ 0 ] );
	if ( date_start == 'NaN' || date_end == 'NaN' ) 
		return banner2.templates [ 'bn-stopped' ];
	if ( today < date_start )
		return banner2.templates [ 'bn-tostart' ];
	if ( today > date_end )
		return banner2.templates [ 'bn-stopped' ];
	if ( date_start < today && date_end > today )
		return banner2.templates [ 'bn-inrange' ];
};

banner2.admin._set_date = function ( frm )
{
	if ( frm.get_value ( "date", "" ) != "" ) banner2.admin.show_months();

	var _date  = frm.get_value ( "date", "" ).replace ( /-/g, "/" ).split ( "|" );
	
    var chk_boxes = $c ( "input", "", "bn_calendar" );
	chk_boxes.iterate ( function ( chk )
	{
		if ( _date.indexOf ( chk.value )  != -1 ) chk.checked = true ;
	});
	
		
}


//{{{banner2.admin.show_months = function ()
banner2.admin.show_months = function ()
{
	var _f = liwe.form.get ( 'bn-banner-form' );
	var _dt_on = _f.get_value ( "data_on" ).substr ( 0, 10 );
	var _dt_off =  _f.get_value ( "data_off" ).substr ( 0, 10 );
	
	if ( _dt_on == _dt_off ) 
	{  
		liwe.utils.notifier.show ( 'Data inizio e data fine sono uguali', 'alert', 2000);
		return;
	};
	
	var x = _dt_on.split ( "-" );
	_dt_on = new Date ( x [ 0 ], x [ 1 ]-1, x [ 2 ] );
	var dt = _dt_on;
	x = _dt_off.split ( "-" );
	_dt_off =  new Date ( x [ 0 ], x [ 1 ]-1, x [ 2 ] );
	var _calendar = new MonthSelector();
	var _month = 0;
	var _last_month = 0;
	var _buf = new String.buffer ();
	var _month_name = {	'1' : 'Gennaio',
					'2' : 'Febbraio',
					'3' : 'Marzo',
					'4' : 'Aprile',
					'5' : 'Maggio',
					'6' : 'Giugno',
					'7' : 'Luglio',
					'8' : 'Agosto',
					'9' : 'Settembre',
					'10' : 'Ottobre',
					'11' : 'Novembre',
					'12' : 'Dicembre'
				};

	var odd_even = [ 'even', 'odd' ]
	var swipe = 0;
	//for ( dt = _dt_on; dt <= _dt_off; dt.setTime ( dt.getTime () + 86400000 ) )
	while ( dt <= _dt_off )
	{
		_month = dt.getMonth() +1;
		if ( _month != _last_month )
		{
			if ( _last_month != 0 )
			{
				_buf.add ( String.formatDict ( banner2.templates [ 'admin-calendar' ] , { 'even_odd':  odd_even [ swipe ]  , 'calendar' : _calendar.render() } ) );
				_calendar.clear ();
				swipe = 1 - swipe;
			}
			_last_month = _month;
			_calendar.nome = _month_name [ _month ];
		}
		_calendar.add ( dt );
		_calendar.prefix = dt.getYear();
		// dt.setTime ( dt.getTime () + 86400000 );
		dt.setDate ( dt.getDate () +1 );
	}

	_calendar.nome = _month_name [ _month ];
	_buf.add ( String.formatDict ( banner2.templates [ 'admin-calendar' ] , { 'even_odd':  odd_even [ swipe ]  , 'calendar' : _calendar.render() } ) );

	$( "bn_calendar" , _buf.toString() );
};
//}}}
//{{{banner2.admin.edit_banner = function ( id )
banner2.admin.edit_banner = function ( id )
{
	banner2.ajax ( { action : "banner2.ajax.get_banner", id_banner : id }, function ( data )
				{
					var vals = data [ 'banner' ]
					banner2.admin._banner_form ( vals );
					vals [ 'width' ] = 200;
					var template = 'bn-admin-image-thumb';
					if ( vals [ 'ext' ] == 'swf' ) template = 'bn-admin-show-swf';
					$( "bn-image-thumb", String.formatDict ( banner2.templates [ template ], vals ) );
					$( "bn-admin-rtitle", "Modifica" );
					window.location.hash = "#bn-admin-rtitle";
					setTimeout ( function () {
						var b = $( 'bn-flash-' + id );
						if ( ! b ) return;
						try
						{
							var w = b.TGetProperty ( '/', 8 );
							var h = b.TGetProperty ( '/', 9 );
							b.height = Math.round ( 200 * h / w );
						} catch ( e ) {
						}
					}, 250 );
					return;
				} );
};
//}}}
//{{{banner2.admin.add_banner = function ()
banner2.admin.add_banner = function ()
{
	banner2.admin._banner_form ();
	window.location.hash = "#bn-admin-rtitle";
};
//}}}
//{{{banner2.admin._banner_refresh = function ( cback )
banner2.admin._banner_refresh = function ( cback )
{
    banner2.admin.am_banner.show_list ();
	banner2.admin._banner_form ();
	if ( cback ) cback();
};
//}}}
//{{{banner2.admin._banner_save =  function ()
banner2.admin._banner_save =  function ()
{
	banner2.admin._banner_refresh ( function () 
	{
		liwe.utils.notifier.show ( 'Banner Salvato correttamente', 'info', 2000)
	});	
};
//}}}
//{{{banner2.admin.del_banner = function ( ids )
banner2.admin.del_banner = function ( ids )
{
	var answer = confirm("Sei sicuro di vole cancellare il Banner?");
	if ( answer )
	{
		var v = { 'action' : 'banner2.admin.del_banner', 'ids' : ids };
		banner2.admin.ajax ( v , function ()
		{
			liwe.utils.notifier.show ( 'Banner Eliminato', 'info', 2000)
			banner2.admin.am_banner.show_list();
		});
	} else {
		return;
	};
};   
//}}} 
//}}}
//{{{GESTIONE GRUPPO 
//{{{banner2.admin._am_gruppo_init = function ( dest_div )
banner2.admin._am_gruppo_init = function ( dest_div )
{
        if ( banner2.admin.am_gruppo ) return;
        banner2.admin.am_gruppo = new AdminManager
                                ( {
                                        'dest_res': dest_div,
                                        'name' : 'bn_am_gruppo',
                                        'list_action': "banner2.ajax.list_gruppo",
                                        'del_action':  "banner2.admin.delete",
                                        'add_action':  "banner2.admin.add_gruppo",
                                        'edit_action':  "banner2.admin.edit_gruppo",
                                        'mod_menu_actions': [
                                                                 { func: "banner2.admin.add_gruppo()", 'class' : 'add' , label: 'Aggiungi Banner' }
					]
                                } );

        banner2.admin.am_gruppo.set_row_fields (
                                [
                                        [ "id", "id" ],
                                        [ "Descrizione", "name" ]
                                ]
                                );

        banner2.admin.am_gruppo.cbacks [ 'row_manip' ] = banner2.admin._rwman_gruppo;
        banner2.admin.am_gruppo.cbacks [ 'del_item' ]  = function ( id ) { banner2.admin.del_gruppo ( id ); };
        banner2.admin.am_gruppo.cbacks [ 'edit_item' ]  = function ( id ) { banner2.admin.edit_gruppo ( id ); };
        banner2.admin.am_gruppo.cbacks [ 'noresult' ] = function () { banner2.admin.add_gruppo (); };
	banner2.admin.am_gruppo.cbacks [ 'pre_del' ] = banner2.admin._gruppo_pre_del;


};
//}}}
//{{{banner2.admin._gruppo_pre_del = function ( am )
banner2.admin._gruppo_pre_del = function ( am )
{
        var ids = am.get_selected ();
        if ( ids == -1 ) return true; 
        ids = "|".join ( ids )
        banner2.admin.del_gruppo ( ids ) 
        return false;
};
//}}}
//{{{banner2.admin._rwman_gruppo = function ( ds, row )
banner2.admin._rwman_gruppo = function ( ds, row )
{
        row [ "_id" ] = row [ 'id' ];
};
//}}}
//{{{banner2.admin._gruppo_form = function ()
banner2.admin._gruppo_form = function ()
{
	var f = new liwe.form.instance ( 'bn-form-gruppo' );
	f.hidden ( 'id_gruppo' , 0 );
	f.events [ 'complete' ] = banner2.admin._gruppo_save;
	f.hidden ( "action", 'banner2.admin.add_gruppo' );
	f.text ( { 'name' : 'name', 'size' : 20, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Gruppo' } );
	f.submit ( { value: 'Salva', "class" : "button green",nonl : true } );
	f.button ( { value: 'Annulla', "class" : "grey", onclick : "banner2.admin._gruppo_refresh()" } );
	f.set ( 'bn-admin-form' );
	$ ( "bn-admin-rtitle", "Inserimento" );
};
//}}}
//{{{banner2.admin.add_gruppo = function ()
banner2.admin.add_gruppo = function ()
{
	banner2.admin._gruppo_form ();
}
//}}}
//{{{banner2.admin.edit_gruppo = function ( id )
banner2.admin.edit_gruppo = function ( id )
{
        var v = { action : 'banner2.ajax.get_gruppo', id_gruppo : id };
        banner2.ajax ( v , function ( data )
        {       
			banner2.admin._gruppo_form();
			var f = liwe.form.get ( 'bn-form-gruppo' );
			gruppo =  data [ 'gruppo' ];
			gruppo [ 'id_gruppo' ] = gruppo [ 'id' ];
			[ 'id_gruppo', 'name' ].iterate ( function ( v, k )
			{
				n = gruppo [ v ];
				if ( typeof ( n ) == 'undefined' ) n = 0;
				f.set_value ( v,n  );
				$ ( "bn-admin-rtitle", "Modifica" );
			});     
        });
};
//}}}
//{{{banner2.admin._gruppo_save =  function ()
banner2.admin._gruppo_save =  function ()
{
	banner2.admin._gruppo_refresh ( function () 
	{
		system.admin.do_alert ( "Gruppo Salvato", liwe.utils.notifier.mode.NOTICE );
		banner2.admin._cache_opts ();
	});	
};
//}}}
//{{{banner2.admin._gruppo_refresh = function ( cback )
banner2.admin._gruppo_refresh = function ( cback )
{
	banner2.admin._gruppo_form ();
        banner2.admin.am_gruppo.show_list ();
	if ( cback ) cback();
};
//}}}
//{{{banner2.admin.del_gruppo = function ( ids )
banner2.admin.del_gruppo = function ( ids )
{
        var v = { 'action' : 'banner2.admin.del_gruppo', 'ids' : ids };
        banner2.admin.ajax ( v, function ()
        {
                system.admin.do_alert ( 'Gruppo Eliminato', liwe.utils.notifier.mode.NOTICE );
                banner2.admin.am_gruppo.show_list ();
        },
        function ( txt, err_descr, err_code, data )
        {
                if ( ! err_descr ) err_descr = txt;
                system.admin.do_alert ( err_descr, liwe.utils.notifier.mode.ALERT );
        });
};
//}}}
//}}}
//{{{ GESTION TIPO 
//{{{banner2.admin._am_tipo_init = function ( dest_div )
banner2.admin._am_tipo_init = function ( dest_div )
{
        if ( banner2.admin.am_tipo ) return;
        banner2.admin.am_tipo = new AdminManager
                                ( {
                                        'dest_res': dest_div,
                                        'name' : 'bn_am_tipo',
                                        'list_action': "banner2.ajax.list_tipo",
                                        'del_action':  "banner2.admin.delete",
                                        'add_action':  "banner2.admin.add_tipo",
                                        'edit_action':  "banner2.admin.edit_tipo",
                                        'mod_menu_actions': [
                                                                 { func: "banner2.admin.add_tipo()", 'class' : 'add' , label: 'Aggiungi Banner' }
					]
                                } );

        banner2.admin.am_tipo.set_row_fields (
                                [
                                        [ "id", "id" ],
                                        [ "Descrizione", "name" ],
                                        [ "Effetto", "effectstr" ],
                                        [ "W", "_w" ],
                                        [ "H", "_h" ],
                                        [ "Playtime", "_playtime" ],
                                        [ "Creato", "created" ]
                                ]
                                );

        banner2.admin.am_tipo.cbacks [ 'row_manip' ] = banner2.admin._rwman_tipo;
        banner2.admin.am_tipo.cbacks [ 'del_item' ]  = function ( id ) { banner2.admin.del_tipo ( id ); };
        banner2.admin.am_tipo.cbacks [ 'edit_item' ]  = function ( id ) { banner2.admin.edit_tipo ( id ); };
        banner2.admin.am_tipo.cbacks [ 'noresult' ] = function () { banner2.admin.add_tipo (); };
	banner2.admin.am_tipo.cbacks [ 'pre_del' ] = banner2.admin._tipo_pre_del;

};
//}}}
//{{{banner2.admin._rwman_tipo = function ( ds, row )
banner2.admin._rwman_tipo = function ( ds, row )
{
        row [ "_id" ] = row [ 'id' ];
        row [ "_w" ] = String.formatDict ( '<div style="text-align:right">%(w)s </div>', row );
        row [ "_h" ] = String.formatDict ( '<div style="text-align:right">%(h)s </div>', row );
        row [ "_playtime" ] = String.formatDict ( '<div style="text-align:right">%(playtime)s </div>', row );
};
//}}}
//{{{banner2.admin._tipo_pre_del = function ( am )
banner2.admin._tipo_pre_del = function ( am )
{
        var ids = am.get_selected ();
        if ( ids == -1 ) return true; 
        ids = "|".join ( ids )
        banner2.admin.del_tipo ( ids ) 
        return false;
};
//}}}
//{{{banner2.admin._tipo_form = function ()
banner2.admin._tipo_form = function ()
{
        var f = new liwe.form.instance ( 'bn-form-tipo' );
        f.hidden ( 'id_tipo' , 0 );
	f.events [ 'complete' ] = banner2.admin._tipo_save;
	f.hidden ( "action", 'banner2.admin.add_tipo' );
        f.text ( { 'name' : 'name', 'size' : 20, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Tipo' } );
	f.select ( { 'name': 'effect', label: 'Effetto',  options: banner2.admin._effect_opts } );
	f.number ( { 'name' : 'playtime', 'size' : 5, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Playtime ( sec )' } );
       	f.number ( { 'name' : 'w', 'size' : 5, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Larghezza ( W )' } );
        f.number ( { 'name' : 'h', 'size' : 5, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Altezza ( H )' } );
        f.submit ( { value: 'Salva', "class" : "button green",nonl : true } );
        f.button ( { value: 'Annulla', "class" : "grey",onclick : "banner2.admin._tipo_refresh()" } );
        f.set ( 'bn-admin-form' );
	$ ( "bn-admin-rtitle", "Inserimento" );
};
//}}}
//{{{banner2.admin.add_tipo = function ()
banner2.admin.add_tipo = function ()
{
	banner2.admin._tipo_form ();
}
//}}}
//{{{banner2.admin.edit_tipo = function ( id_tipo )
banner2.admin.edit_tipo = function ( id_tipo )
{
        var v = { action : 'banner2.ajax.get_tipo', id_tipo : id_tipo };
        banner2.ajax ( v , function ( data )
        {       
			banner2.admin._tipo_form();
			var f = liwe.form.get ( 'bn-form-tipo' );
			tipo =  data [ 'tipo' ];
			tipo [ 'id_tipo' ] = tipo [ 'id' ];
			[ 'id_tipo', 'name','effect' ,'playtime', 'w', 'h' ].iterate ( function ( v, k )
			{
				n = tipo [ v ];
				if ( typeof ( n ) == 'undefined' ) n = 0;
				f.set_value ( v,n  );
				$ ( "bn-admin-rtitle", "Modifica" );
			});     
        });
};
//}}}
//{{{banner2.admin._tipo_save =  function ()
banner2.admin._tipo_save =  function ()
{
	banner2.admin._tipo_refresh ( function () 
	{
		system.admin.do_alert ( "tipo Salvato", liwe.utils.notifier.mode.NOTICE );
		banner2.admin._cache_opts ();
	});	
};
//}}}
//{{{banner2.admin.del_tipo = function ( ids )
banner2.admin.del_tipo = function ( ids )
{
        var v = { 'action' : 'banner2.admin.del_tipo', 'ids' : ids };
        banner2.admin.ajax ( v, function ()
        {
                system.admin.do_alert ( 'Tipo Eliminato', liwe.utils.notifier.mode.NOTICE );
                banner2.admin.am_tipo.show_list ();
        },
        function ( txt, err_descr, err_code, data )
        {
                if ( ! err_descr ) err_descr = txt;
                system.admin.do_alert ( err_descr, liwe.utils.notifier.mode.ALERT );
        });
};
//}}}
//{{{banner2.admin._tipo_refresh = function ( cback )
banner2.admin._tipo_refresh = function ( cback )
{
	banner2.admin._tipo_form ();
        banner2.admin.am_tipo.show_list ();
	if ( cback ) cback();
};
//}}}
//}}}
//{{{ STATISTICHE
//{{{banner2.admin._render_stats = function ()
banner2.admin._render_stats = function ()
{
	banner2.admin._render_panel ( 'bn-admin-stats', 'Statistiche', function ()
	{
		banner2.admin._stats_refresh();
	});
};
//}}}
//{{{banner2.admin._stats_refresh = function ()
banner2.admin._stats_refresh = function ()
{
	banner2.admin.ajax ( { action: 'banner2.ajax.list_gruppo', _X_LINES: -1 }, function ( res )
	{
		var rows = res [ 'rows' ], groups = {};
		rows.iterate ( function ( v )
		{
			groups [ v [ 'id' ] ] = v;
		} );

		banner2.admin.ajax ( { action: 'banner2.ajax.list_banner', _X_LINES: -1 }, function ( res )
		{
			var extras = { label: 'Banner' };
			var rows = res [ 'rows' ];

			rows.iterate ( function ( b )
			{
				var g = groups [ b [ 'id_gruppo' ] ];

				if ( ! g [ 'ids' ] ) g [ 'ids' ] = [];
				if ( ! g [ 'items' ] ) g [ 'items' ] = [];

				g [ 'ids' ].push ( b [ 'id_banner' ] );
				g [ 'items' ].push ( b );
			} );

			var data = [];
			groups.iterate ( function ( g )
			{
				if ( g [ 'ids'].length == 0 ) return;

				data.push ( { label: '&raquo;&nbsp;' + g [ 'name' ] + '', value: g [ 'ids' ].join ( ',' ) } );

				g [ 'items' ].iterate ( function ( b )
				{
					data.push ( { label: '&nbsp;&middot;&nbsp;' + b [ 'banner' ], value: b [ 'id_banner' ] } );
				} );
			} );

			extras [ 'data' ] = data;

			stats.form ( 'bn-admin-stats-form', 'bn-admin-stats-graph', extras );
		} );
	} );
};
//}}}
//}}}
//{{{JCONED PLUGIN
//{{{banner2.admin._jconed_form = function ( values, data )
banner2.admin._jconed_form = function ( values, data )
{
	if ( ! data ) data = {};

	if ( ! data.get ( 'banners' ) )
	{
		var params = { 
			action : 'banner2.ajax.list_banner',
			_X_LINES : -1
		};
		banner2.admin.ajax ( params, function ( res )
		{
			data [ 'banners' ] = res.rows;
			banner2.admin._jconed_form ( values, data );
		} );
		return;
	}

	if ( ! data.get ( 'tipi' ) )
	{
		var params = { 
			action : 'banner2.ajax.list_tipo',
			_X_LINES : -1
		};
		banner2.admin.ajax ( params, function ( res )
		{
			data [ 'tipi' ] = res.rows;
			banner2.admin._jconed_form ( values, data );
		} );
		return;
	}

	values = values || {};

	var acc = new WWL.accordion ( 'jconed-banner' );
	var opts = [], f;

	data [ 'tipi' ].iterate ( function ( v, k ) 
	{
		opts.push ( { label: v.name, value : v.id } );			
	} );

	f = new liwe.form.instance ( 'jconed-banner-tipo' );
	f.hidden ( '_module', 'banner2' );
	f.hidden ( '_title', 'Banner Tipo' );
	f.select ( { name: 'id_tipo', options: opts, force_select: true, mandatory: true, label: 'Gruppo', value: values.get ( 'id_tipo' ) } );
	f.button ( { value: "Conferma", onclick: "jconed.render_plugin('jconed-banner-tipo')" } );

	acc.add_item (
	{
		'txt': 'Tipo Banner',
		'content': f.get ()
	} );

	opts = [];
	data [ 'banners' ].iterate ( function ( v, k ) 
	{
		opts.push ( { label: v.banner, value : v.id_banner } );
	} );

	f = new liwe.form.instance ( 'jconed-banner' );
	f.hidden ( '_module', 'banner2' );
	f.hidden ( '_title', 'Singolo Banner' );
	f.select ( { name: 'id', options: opts, force_select: true, mandatory: true, label: 'Banner', value: values.get ( 'id' ) } );
	f.button ( { value: "Conferma", onclick: "jconed.render_plugin('jconed-banner')" } );

	acc.add_item (
	{
		'txt': 'Singolo Banner',
		'content': f.get ()
	} );

	jconed.toolbar.set_context ( acc.to_string () );

	acc.click ( values.get ( 'id' ) ? 2 : 1 );
	
};
//}}}
//{{{banner2.admin._jconed_handler = function ()
banner2.admin._jconed_handler = function ()
{
	banner2.admin._jconed_form ();
};
//}}}
//{{{banner2.admin._jconed_context = function ()
banner2.admin._jconed_context = function ()
{
        var item = jconed.get_plugin_selection ();
        if ( ! item ) return;
	if ( ! liwe.dom.has_class ( item, 'liwe-plugin-banner2' ) ) return;

        pre = item.getElementsByTagName ( "pre" );
        if ( ! pre.length ) return;

        pre = pre [ 0 ];

	var values = null;
        eval ( 'values = ' + pre.innerHTML );

	//if ( typeof ( values ) != 'object' ) return; 

	banner2.admin._jconed_form ( values [ 'params' ] );
};
//}}}
//}}}

